// pages/operation/class/classInfo/index.js
import { ClassModel } from "../../../../api/api-work/classModel.js"
import { throttle } from "../../../../utils/index.js"
const classModel = new ClassModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    pageNum: 1,
    total: 0,
    tableDatas: [],
    loadStatus: "",
    triggered: false,


    showClassFilter: false, // 班课筛选
    classForm: {
      f1: false, // 只看我的班级
      f2: "1", // 未结业
      f3: false, // 是否排课
      f4: "1", // 在线选班
      f5: '1', // 充值课消
      f6: "1", // 课程
      f6Name: "请选择",
      f7: "1", // 老师
      f7Name: "请选择",
      f8: "1", // 教室
      f8Name: "请选择",
      f9: "1",
      f9Name: "请选择"
    },
    showCommonList: false,
    commonTitle: "",
    commonProp: "",
    commonLists: [],
    commonSelectId: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // await this.getClassList()
  },

  searchClass: throttle(function (e) {
    this.setData({
      pageNum: 1,
      tableDatas: []
    })
    this.getClassList()
  }, 500),


  // searchClass: throttle() {
  //   this.setData({
  //     pageNum: 1,
  //     tableDatas: []
  //   })
  //   console.log("haha")
  //   this.getClassList()
  // },

  // 班级列表
  async getClassList() {
    classModel.getClassListApi({ pageNum: this.data.pageNum, pageSize: 15, ifMine: 1, name: this.data.name }).then(res => {
      let rows = this.data.tableDatas;
      rows.push(...res.rows);
      this.setData({
        tableDatas: rows,
        total: res.total
      })
      if (this.data.total == 0) {
        this.setData({
          loadStatus: ""
        })
      }
      else if (this.data.tableDatas.length < this.data.total) {
        this.setData({
          loadStatus: "loadmore"
        })
      }
      else {
        this.setData({
          loadStatus: "nomore"
        })
      }
    })
  },
  getMoreClass() {
    if (this.data.loadStatus === "nomore") {
      return;
    }
    this.setData({
      pageNum: this.data.pageNum + 1,
      loadStatus: "loading"
    })
    this.getClassList();
  },
  refreshMoreClass() {
    this.setData({
      triggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getClassList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      triggered: false,
      pageNum: 1,
      tableDatas: []
    })
    this.getClassList()
  },

  // 班级筛选 弹出
  filterClassInfo() {
    this.setData({
      showClassFilter: true,
      checked: true
    })
  },

  // 只看我的班级
  onChangeF1(e) {
    this.setData({
      'classForm.f1': e.detail
    })
  },

  onChangeF3(e) {
    this.setData({
      'classForm.f3': e.detail
    })
  },

  // 课程/老师/教室筛选
  classFilterCellClick(e) {

    const query = e.currentTarget.dataset.query;
    console.log("sssffdfd: ", query)
    if (query == 'f6') {
      // 课程
      this.setData({
        showCommonList: true,
        commonTitle: "课程",
        commonProp: query, // 属性名
        commonSelectId: this.data.classForm.f6,
        commonLists: [
          {
            id: "1",
            data: "课程价目表"
          },
          {
            id: "2",
            data: "299特价小课包"
          },
          {
            id: "3",
            data: "144课程包"
          },
          {
            id: "4",
            data: "新2021两年"
          }
        ]
      })
    }
    else if (query == 'f7') {
      // 老师
      this.setData({
        showCommonList: true,
        commonTitle: "老师",
        commonProp: query, // 属性名
        commonSelectId: this.data.classForm.f7,
        commonLists: [
          {
            id: "1",
            data: "老师1"
          },
          {
            id: "2",
            data: "老师2"
          },
          {
            id: "3",
            data: "老师3"
          },
          {
            id: "4",
            data: "老师4"
          }
        ]
      })
    }
    else if (query == 'f8') {
      // 教室
      this.setData({
        showCommonList: true,
        commonTitle: "教室",
        commonProp: query, // 属性名
        commonSelectId: this.data.classForm.f8,
        commonLists: [
          {
            id: "1",
            data: "教室1"
          },
          {
            id: "2",
            data: "教室2"
          },
          {
            id: "3",
            data: "教室3"
          },
          {
            id: "4",
            data: "教室4"
          }
        ]
      })
    }
    else if (query == 'f9') {
      // 班级
      this.setData({
        showCommonList: true,
        commonTitle: "班级",
        commonProp: query, // 属性名
        commonSelectId: this.data.classForm.f9,
        commonLists: [
          {
            id: "1",
            data: "班级1"
          },
          {
            id: "2",
            data: "班级2"
          },
          {
            id: "3",
            data: "班级3"
          },
          {
            id: "4",
            data: "班级4"
          }
        ]
      })
    }
  },

  // 选中回调
  selectCommonCellBlock(e) {
    console.log("sssdddd: ", e)
    const obj = e.detail.value;

    if (e.detail.prop == 'f6') {
      // 课程
      this.setData({
        showCommonList: false,
        "classForm.f6": obj.id,
        "classForm.f6Name": obj.data
      })
    }
    else if (e.detail.prop == 'f7') {
      // 老师
      this.setData({
        showCommonList: false,
        "classForm.f7": obj.id,
        "classForm.f7Name": obj.data
      })
    }
    else if (e.detail.prop == 'f8') {
      // 教室
      this.setData({
        showCommonList: false,
        "classForm.f8": obj.id,
        "classForm.f8Name": obj.data
      })
    }
    else if (e.detail.prop == 'f9') {
      // 班级
      this.setData({
        showCommonList: false,
        "classForm.f9": obj.id,
        "classForm.f9Name": obj.data
      })
    }

  },

  closeCommonListClick() {
    this.setData({
      showCommonList: false
    })
  },

  // 关闭班级筛选
  closeClassFilter() {
    this.setData({
      showClassFilter: false
    })
  },

  // 添加/修改班级信息
  gotoAddClassInfo() {
    wx.navigateTo({
      url: '/pages/operation/class/classInfoEdit/index',
    })
  },

  // 班课详细
  gotoClassInfoList(e) {
    wx.navigateTo({
      url: `/pages/operation/class/classInfoList/index?id=${e.currentTarget.dataset.query.id}&beOver=${e.currentTarget.dataset.query.beOver}`,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})